package com.yulebaby.callsenter.ui.activity;

import android.content.Intent;
import android.os.Environment;
import android.os.Handler;
import android.text.TextUtils;
import android.widget.ImageView;

import com.yulebaby.callsenter.R;
import com.yulebaby.callsenter.bean.CallLogBean;
import com.yulebaby.callsenter.bean.PreSaleRemarkBean;
import com.yulebaby.callsenter.bean.ResultBean;
import com.yulebaby.callsenter.database.manager.Dao;
import com.yulebaby.callsenter.event.EventFactory;
import com.yulebaby.callsenter.event.RefreshEvent;
import com.yulebaby.callsenter.global.Constant;
import com.yulebaby.callsenter.http.callback.HttpCallback;
import com.yulebaby.callsenter.record.PhoneService;
import com.yulebaby.callsenter.task.UploadCallLogTask;
import com.yulebaby.callsenter.task.UploadPreSaleRemarkTask;
import com.yulebaby.callsenter.ui.base.BaseActivity;
import com.yulebaby.callsenter.utils.ImageLoader;
import com.yulebaby.callsenter.utils.JpushUtil;
import com.yulebaby.callsenter.utils.SPUtil;
import com.yulebaby.callsenter.utils.ThreadUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;


public class SplashActivity extends BaseActivity {


    @BindView(R.id.iv_splash)
    ImageView mIvSplash;
    private Dao mDao;
    private int User_id;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initView() {
        //跳转判断是否显示登录界面
        ThreadUtils.runOnUiThreadDelay(new Runnable() {
            @Override
            public void run() {
                String user_token = (String) SPUtil.get(Constant.USER_TOKEN, "");
                int user_id = (int) SPUtil.get(Constant.USER_ID, -1);
                if (TextUtils.isEmpty(user_token) || user_id == -1) {
                    startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                } else {
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    EventBus.getDefault().post(EventFactory.CreateRefreshEvent(RefreshEvent.refreshDataFromInternet));
                    startService(new Intent(SplashActivity.this, PhoneService.class));

                }
                finish();
            }
        }, 2000);
    }

    /**
     * 初始化数据
     */
    @Override
    protected void initData() {
        mDao = Dao.getInstance();
        ImageLoader.load(this, R.drawable.splash, mIvSplash);
        ThreadUtils.runOnBackgroundThread(() -> {
                    List<CallLogBean> callLogBeanList = mDao.listUnloadFailAll();
                    int isSuccess = (int) SPUtil.get(Constant.REGISTER_ALIAS_SUCCESS, -1);  //极光
                    if (isSuccess == -1) {
                        setAlias();
                    }
                    ThreadUtils.runOnUiThread(() -> {
                        if (callLogBeanList != null) {
                            UploadCallLogTask.doUploadListFiles(callLogBeanList);  //上传未成功上传的通话录音
                        }
                    });
                }

        );
    }


    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {

    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {

    }

    /**
     * 保存别名
     */
    private void setAlias() {
        int customerAlias = (int) SPUtil.get(Constant.CUSTOMER_ALIAS, -1);
        User_id = (int) SPUtil.get(Constant.USER_ID, -1);
        if (customerAlias == -1 && User_id != -1) {
            mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_ALIAS, String.valueOf(User_id)));
        }
    }


    private static final int MSG_SET_ALIAS = 1001;
    private static final int MSG_SET_TAGS = 1002;

    /**
     * 极光
     */
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_SET_ALIAS:
                    JPushInterface.setAliasAndTags(getApplicationContext(), (String) msg.obj, null, mAliasCallback);
                    break;
                case MSG_SET_TAGS:
                    break;
                default:
            }
        }
    };


    private TagAliasCallback mAliasCallback = new TagAliasCallback() {
        @Override
        public void gotResult(int code, String alias, Set<String> tags) {
            switch (code) {
                case 0:
                    SPUtil.save(Constant.CUSTOMER_ALIAS, User_id);
                    break;

                case 6002:
                    if (JpushUtil.isConnected(getApplicationContext())) {
                        mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SET_ALIAS, alias), 1000 * 60);
                    } else {
                        SPUtil.remove(Constant.CUSTOMER_ALIAS);
                    }
                    break;

                default:
            }
        }
    };
}
